<?php
/**
 * 消费订单提交接口
 */
namespace Api\Controller;

use Think\Controller;

class SaleController extends Controller {
	
	public function _initialize() {
        $this->m_card_sale = D("Sale/CardSale");
	    $this->m_sale_good = D("Sale/SaleGood");
	    // $this->m_card_sale_cancel = D("Sale/CardSaleCancel");
	    // $this->m_sale = D("Admin/Sale");
	    // $this->m_sale_manager = D("Admin/SaleManager");
	}

    public function index() {
    	
    	exit(json_encode(array('x'=>'y')));
    }

    /*获取销售列表+分页*/
    public function entry() {
	    $sale_id = I("post.sale_id");
	    // $sale_id = 18;
	    $index = intval(I("request.index"));
	    
        $where = array("s.sale_id"=>$sale_id);

        $pagesize = 20;
        $page = $index * $pagesize;

    	$result = $this->m_card_sale->get_list($where ,"create_time DESC" , $page, $pagesize);

    	$list = array();
    	foreach ($result as $_key => $_row) {
    		$list[] = array(
    			'detail_id'=>$_row['id'],
    			'goods_name'=>$_row['goods_name'].'-'.$_row['goods_num'].$_row['goods_unit'],
    			'sale_time'=>date('Y-m-d H:i:s',$_row['create_time']),
    			'price'=>$_row['price'],
    		);
    	}

    	exit(json_return('1','',array('list'=>$list,'count'=>count($list))));
    }

    /*销售前计算*/
    public function precalc() {
	    $sale_id = I("post.sale_id");	// 消费机构ID
        $good_id = I("post.good_id");   // 商品ID
	    $goods_price = I("post.goods_price");	// 收费价格
        $good_num = I("post.good_num"); // 商品数量
	    $last_key = I("post.last_key");	// 最后修改的数据类型:preprice|good_num

        $result = $this->m_card_sale->pre_clac($sale_id, $good_id, $goods_price, $good_num, $last_key, 'is_vip');
        if($result === -1){
            exit(json_return('0','会员卡信息获取失败！'));
        }elseif($result === -2){
            exit(json_return('0','会员卡余额不足！'));
        }elseif($result === -3){
            exit(json_return('0','该会员卡已注销，无法消费！'));
        }elseif($result === -4){
            exit(json_return('0','上级消费机构余额不足，无法消费！'));
        }elseif($result === -5){
            exit(json_return('0','商品信息获取失败！'));
        }
    	exit(json_return('1','',$result));
    }

    /*销售详情*/
    public function detail() {
    	$detail_id = I("request.detail_id");

    	// $detail_id = 91;

    	$sale_data = $this->m_card_sale->where(array("id"=>$detail_id))->find();
    	$sale_data['total_add_point'] = $sale_data['general_point'] + $sale_data['self_point'];
        $sale = M("Sale")->where(array("id"=>$sale_data['sale_id']))->find();
    	$sale_user = M('users')->where(array('id'=>$sale_data['uid']))->find();

    	$data = array(
            'title' => '刷卡消费单',
            'detail_id' => $detail_id,
            'content' =>array(
    			array(
                    'name' => '流水号',
                    'value' => $sale_data['number'],
                ),
    			array(
                    'name' => '消费时间',
                    'value' => date("Y-m-d H:i:s",$sale_data['create_time']),
                ),
    			array(
                    'name' => '消费商品',
                    'value' => $sale_data['goods_name'],
                ),
    			array(
                    'name' => '商品单价',
                    'value' => $sale_data['goods_unit']?($sale_data['goods_unit_price'].'/'.$sale_data['goods_unit']):$sale_data['goods_unit_price'],
                ),
    			array(
                    'name' => '商品数量',
                    'value' => $sale_data['goods_num'].$sale_data['goods_unit'],
                ),
    			array(
                    'name' => '订单总价',
                    'value' => $sale_data['goods_price'].'',
                ),
    			array(
                    'name' => '优惠金额',
                    'value' => $sale_data['dis_price'].'',
                ),
    			array(
                    'name' => '实付金额',
                    'value' => $sale_data['price'].'',
                ),
    			array(
                    'name' => '消费卡号',
                    'value' => $sale_data['card_number'],
                ),
    			array(
                    'name' => '卡内余额',
                    'value' => $sale_data['plus_price'],
                ),
    			array(
                    'name' => '本次积分',
                    'value' => $sale_data['total_add_point'],
                ),
    			array(
                    'name' => '卡内积分',
                    'value' => $sale_data['plus_point'],
                ),
    			array(
                    'name' => '消费机构',
                    'value' => $sale['sale_name'],
                ),
    			array(
                    'name' => '操作员',
                    'value' => $sale_user['user_nicename'],
                ),
            ),
    	);
    	exit(json_return('1','',$data));
    }

    /*卡片消费*/
    public function salepost(){
        $sale_id = I("post.sale_id");
        $user_id = I("post.user_id");
        $card_number = I("post.card_number");
        $goods_price = I("post.goods_price");
        $good_num = I("post.good_num");
        $good_id = I("post.good_id");
        $last_key = I("post.last_key");
        $desc = I("post.desc");

        if(empty($good_id)){
            exit(json_return('0','请先选择商品！'));
        }
        if(empty($card_number)){
            exit(json_return('0','请先扫卡！'));
        }
        if(empty($good_num)){
            exit(json_return('0','请输入商品数量！'));
        }
        $sale_good = $this->m_sale_good->where(array("id"=>$good_id))->find();
        if(empty($sale_good)){
            exit(json_return('0','您选择的商品不存在，请重新选择！'));
        }
        $result = $this->m_card_sale->insert_data($sale_id, $card_number, $goods_price, $good_num, $desc, $sale_good, $last_key, $user_id);

        if($result === -1){
            exit(json_return('0','会员卡信息获取失败！'));
        }elseif($result === -2){
            exit(json_return('0','会员卡余额不足！'));
        }elseif($result === -3){
            exit(json_return('0','上级消费机构余额不足，无法消费！'));
        }elseif($result === -4){
            exit(json_return('0','该会员卡已注销，无法消费！'));
        }elseif($result === -5){
            exit(json_return('0','商品信息获取失败！'));
        }elseif($result === -6){
            exit(json_return('0','消费机构查询失败，请重新登录！'));
        }
        if ($result) {
            push_sale($result);
            exit(json_return('1','',array('detail_id'=>$result)));
        } else {
            exit(json_return('0','消费失败！'));
        }
    }
}

